package com.zhouchaoqun.web.easyexcel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * @author wangbao02
 * @date 2019-03-14 10:09
 */
@Slf4j
public class ExpressBillExcelListener extends AnalysisEventListener<ExpressBillModel> {

    private List<ExpressBillModel> list = new ArrayList<>();
    private Map<Integer, String> headMap = null;
    private CountDownLatch latch;

    public ExpressBillExcelListener(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void invoke(ExpressBillModel data, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        list.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        if (context.readWorkbookHolder() != null
                && context.readWorkbookHolder().getInputStream() != null) {
            context.readWorkbookHolder().getInputStream().close();
        }
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        this.headMap = headMap;
        latch.countDown();
    }


    public List<ExpressBillModel> getList() {
        return list;
    }

    public Map<Integer, String> getHeadMap() {
        return headMap;
    }
}

